package com.yocard.yxyd.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.yocard.yxyd.utils.util.ActivityUtils;
import com.yocard.yxyd.utils.util.ConvertUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yocard.yxyd.R;
import com.yocard.yxyd.adapter.TransactionAdapter;
import com.yocard.yxyd.base.AppContext;
import com.yocard.yxyd.base.Constants;
import com.yocard.yxyd.http.model.response.CoinsBean;
import com.yocard.yxyd.ui.activity.TransDetailActivity;
import com.yocard.yxyd.ui.contract.TransContract;
import com.yocard.yxyd.ui.fragment.base.BaseFragment;
import com.yocard.yxyd.ui.presenter.TransPresenter;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created on 2019/4/10.
 * 交易记录fragment 19/05/04
 */
public class AllTransactionFragment extends BaseFragment<TransPresenter> implements TransContract.View {

    @BindView(R.id.recycle_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    List<CoinsBean> datas = new ArrayList<>();
    protected int pos;
    int currentPage;
    protected TransactionAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ft_transaction;
    }

    @Override
    protected void initEventAndData() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            pos = (int) arguments.get("pos");
        }

        HorizontalDividerItemDecoration horizontalBuild = new HorizontalDividerItemDecoration.Builder(mContext)
                .color(AppContext.getColor_(R.color.color2))
                .size(ConvertUtils.dp2px(10))
                .build();
        mAdapter = new TransactionAdapter(datas);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(horizontalBuild);
        recyclerView.setAdapter(mAdapter);

        refreshLayout.setOnRefreshListener(refreshLayout1 -> {
            currentPage = 1;
            mPresenter.fetchList(currentPage,getType());
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout1 -> {
            currentPage++;
            mPresenter.fetchList(currentPage,getType());
        });

        mAdapter.setOnItemClickListener(value -> {
            Bundle bundle = new Bundle();
            bundle.putSerializable("data",value);
            ActivityUtils.startActivity(bundle, TransDetailActivity.class);
        });

        showLoadingDialog();
        currentPage = 1;
        mPresenter.fetchList(currentPage,getType());
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    private String getType(){
        String type = "";
        switch (pos){
            case 1:
                type = "income";
                break;
            case 2:
                type = "outcome";
                break;
        }
        return type;
    }

    @Override
    public void onFetchList(List<CoinsBean> list) {
        if (currentPage == 1) {
            datas.clear();
            datas.addAll(list);
            refreshLayout.finishRefresh();
            refreshLayout.resetNoMoreData();
            mAdapter.notifyDataSetChanged();
            dismisDialog();
        } else {
            datas.addAll(list);
            refreshLayout.finishLoadMore();
            if (list.size() < Constants.PAGE_SIZE) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showErrorMsg(String msg) {
        super.showErrorMsg(msg);
        refreshLayout.finishRefresh();
    }
}
